<?php 

class Model{

	protected static $_instance;
	protected static $_db_con;

	protected $_table;
	protected $_condition = 1;
	protected $_fields = '*';
	protected $_limit;
	protected $_order_by  = ' id DESC ';
	protected $_data;
	
	protected $_rules = array();
	
	//validate errors
	protected $_errors;
	

	public function __construct($table=null){
		if(!self::$_db_con){
			$db_host = C('DB_HOST');
			$db_user = C('DB_USER');
			$db_pass = C('DB_PASS');
			$db_name = C('DB_NAME');
			$charset = C('charset');
			$con = mysql_connect($db_host,$db_user,$db_pass);
			if(!$con){
				exit('database connect error.');
			}
			mysql_select_db($db_name);
			mysql_query($charset);
			
			self::$_db_con = $con;
		}
	}
	
	public function table($table){
		$this->_table = $table;
		return $this;
	}
	
	public function select(){
		$sql = "SELECT $this->_fields FROM $this->_table WHERE $this->_condition ORDER BY $this->_order_by $this->_limit ";
		$result = mysql_query($sql);
		$r = array();
		while($row = mysql_fetch_assoc($result)){
			$r[] = $row;
		}
		return $r;
	}
	
	public function find($id=null){
		if($id){
			$condition = " id = '$id' ";
		}else{
			$condition = $this->_condition;
		}
		$sql = "SELECT $this->_fields FROM $this->_table WHERE $condition LIMIT 1 ";
		$result = mysql_query($sql);
		$row = mysql_fetch_assoc($result);
		return $row;
	}
	
	public function where($condition){
		$this->_condition = $condition;
		return $this;
	}
	
	public function field($fields){
		$this->_fields = $fields;
		return $this;
	}
	
	public function page($page,$limit){
		$start = ($page-1)*$limit;
		$this->_limit = " LIMIT $start,$limit ";
		return $this;
	}
	
	public function order($order_by){
		$this->_order_by = $order_by;
		return $this;
	}
	
	public function create($data){
		$fields = $this->_getFields();
		foreach($data as $key=>$value){
			if(in_array($key,$fields)){
				$this->_data[$key] = $value;
				//validate the data
				$this->_validate($key,$value);
			}
		}
		
		if(empty($this->_errors)){
			return true;
		}else{
			return false;
		}
	}
	
	public function add($data=null){
		$fields = $this->_getFields();
		$item = empty($data)?$this->_data:$data;
		if(empty($item)){
			exit('no data to insert');
		}
		
		$keys = array();
		$values = array();
		foreach($item as $key=>$value){
			if(in_array($key,$fields)){
				$keys[] = $key;
				$values[] = "'".$value."'";
			}
		}
		
		$sql = "INSERT INTO $this->_table ( ".implode(',',$keys).") 
		VALUES ( ".implode(',',$values)." ) ";		
		$r = mysql_query($sql) or die(mysql_error());
		return $r;
		
	}
	
	public function save($data=null){
		$data = empty($data)?$this->_data:$data;
		$condition = array();
	
		foreach($data as $key=>$value){
			$condition[] = " $key = '$value' ";
		}
		
		$sql = " UPDATE $this->_table SET ".implode(',',$condition).
		" WHERE $this->_condition LIMIT 1 ";
		$r = mysql_query($sql);
		return $r;
	}
	
	//delete data
	public function delete($ids){
		$sql = " DELETE FROM $this->_table WHERE id IN ( $ids ) ";
		$r = mysql_query($sql);
		return $r;
	}
	
	//get a error
	public function getError(){
		return $this->_errors[0];
	}
	
	//get all errors
	public function getAllErrors(){
		return $this->_errors;
	}
	
	protected function _getFields(){
		$sql = " SELECT COLUMN_NAME from information_schema.COLUMNS where 
		table_name = '$this->_table' AND table_schema = 'test' ";
		$r = mysql_query($sql);
		$fields = array();
		while($row = mysql_fetch_assoc($r)){
			$fields[] = $row['COLUMN_NAME'];
		}
		return $fields;
	}
	
	//validate data,this is a complex function 
	protected function _validate($key,$value){
		$rules = $this->_rules;
		if(isset($rules[$key])){
			$rule = $rules[$key];
			if($rule['condition'] == 'required' ){
				if(trim($value)==''){
					$this->_errors[] = $rule['label'];
				}
			}else if($rule['condition'] == 'number' ){
				if(!is_numeric($value)){
					$this->_errors[] = $rule['label'];
				}
			}
		}
	}
}


?>